#include <cstdlib>
#include <iostream>
#include <fstream>
#include <windows.h>

using namespace std;

extern int UserStat[8];
extern int DeptStat[8];
extern string Name;
extern int Pass;
extern int PwrStat[10];
extern int DeptPwr[10];

ifstream StatRead;  
ofstream StatWrite;

void Encrypt(int Pwd)
{
     for(int x=0; x<8; x++)
     {
             UserStat[x] = DeptStat[x] * Pwd;
     } 
}

void Decrypt(int Pwd)
{
     for(int x=0; x<8; x++)
     {
             DeptStat[x] = UserStat[x] / Pwd;
     }
}

void EncryptPwr(int Pwd)
{
     for(int x=0; x<10; x++)
     {
             PwrStat[x] = DeptPwr[x] * (20*Pwd);
     } 
}

void DecryptPwr(int Pwd)
{
     for(int x=0; x<10; x++)
     {
             DeptPwr[x] = PwrStat[x] / (20*Pwd);
     }
}

void UserWrite()
{
   StatWrite.open("usr.dat");
   Encrypt(DeptStat[1]);
   for(int x=0; x<8; x++)
   {
           StatWrite << UserStat[x] << endl;
   }
   StatWrite.close();
}

void UserInit()
{
     int Pwd;
     cout << "Please input your name (No Space): ";
     cin >> Name;
     cout << "\nWelcome " << Name << "!\n";
     cout << "Please input your Password (xxxx): ";
     cin >> Pwd;
     Pass = Pwd;
     cout << endl;
     ofstream Init;
     ofstream SaveName;
     SaveName.open("name.dat");
     Init.open("usr.dat");
     for (int x=0; x<8; x++)
     {
         if(x!=1)
         {
         DeptStat[x] = 0;
         }
     }
     DeptStat[1] = Pwd;
     DeptStat[2] = 100;
     DeptStat[3] = 1000;
     DeptStat[4] = 1;
     DeptStat[5] = 20;
     DeptStat[6] = 18;
     DeptStat[7] = 0;
     UserWrite();
     SaveName << Name;
     SaveName.close();
     Init.close();
}

void PwrWrite()
{
     ofstream Pwr;
     Pwr.open("pwr.dat");
     EncryptPwr(Pass);
     for(int x=0; x<10; x++)
     {
             Pwr << PwrStat[x] << endl;
     }
     Pwr.close();
}

void PwrInit()
{
     for(int x=0; x<10; x++)
     {
             DeptPwr[x] = 0;
     }
     DeptPwr[0] = 10;
     DeptPwr[1] = 5;
     PwrWrite();
}

void PwrRead()
{
     ifstream PwrRead;
     PwrRead.open("pwr.dat",ios::in);
     if(!PwrRead)
     {
                MessageBox (NULL, "pwr.dat not found!"  , "Error!" , 0);
                system("pause");
                exit(1);
     }
     for(int x=0; x<10; x++)
     {
             PwrRead >> PwrStat[x];
     }
     PwrRead.close();
     DecryptPwr(Pass);
}

void UserRead(bool First)
{
    int Tmp;
    if(First)
    {
             UserInit();
             PwrInit();
    }
    else
    {
        StatRead.open("usr.dat",ios::in);
        if(!StatRead)
        {
                MessageBox (NULL, "usr.dat not found!"  , "Error!" , 0);
                system("pause");
                exit(1);
        }
        for(int x=0; x<8; x++)
        {
            StatRead >> UserStat[x];
        }
        StatRead.close();
    cout << "Please input the Password to Decrypt your Profile (xxxx): ";
    cin >> Tmp;
    
    if((UserStat[6] / Tmp) == 18)
      {
        Pass = Tmp;
        Decrypt(Tmp);
      }
    else
      {
       cout << "\nThis is not a right Password!\n";
       system("pause");
       exit(1);
      }
    }
    
}
void ReadName()
{
     ifstream ReadName;
     ReadName.open("name.dat",ios::in);
     if(!ReadName)
     {
     MessageBox (NULL, "name.dat not found!"  , "Error!" , 0);
     system("pause");
     exit(1);
     }
     ReadName >> Name;
     ReadName.close();
}
